import { _decorator, Component, find, Node, tween, Vec3, log, error, director, UITransform } from 'cc';
import { GYComponent } from '../Core/组件基类/GYComponent';
const { ccclass, property } = _decorator;

@ccclass('UIBase')
export class UIBase extends GYComponent {


    @property(Node)
    public maskLayer: Node = null;

    @property({
        type: Node,
        tooltip: '动画层',
        displayName: '动画层'
    })
    public zoomPanel: Node = null;

    start() {
        window["UIBase"] = UIBase;
    }

    protected onEnable(): void {
        this.adjustMaskLayer();
    }

    show() {
        this.node.active = true;
    }

    hide() {
        this.node.active = false;
    }

    protected onDestroy(): void {
        Guyu.dispatcher.offAllCaller(this);
    }

    adjustMaskLayer() {
        this.maskLayer = this.node.getChildByName("MaskLayer");
        if (!this.maskLayer) {
            return;
        }
        // 设置maskLayer的大小与Canvas一致
        const canvas = director.getScene().getChildByName('Canvas');
        if (canvas && this.maskLayer) {
            const canvasSize = canvas.getComponent(UITransform).contentSize;
            const maskTransform = this.maskLayer.getComponent(UITransform);
            if (maskTransform) {
                maskTransform.setContentSize(canvasSize);
                // this.maskLayer.setPosition(new Vec3(0, 0, 0));
            }
        }
    }

    zoom(node?: Node, mode: "zoomIn" | "zoomOut" = "zoomIn") {
        if (!node) {
            node = this.zoomPanel || this.node;
        }
        if (!node) {
            error("zoom node is null");
            return;
        }
        if (mode == "zoomIn") {
            // zoomIn
            node.setScale(new Vec3(1.5, 1.5, 1.5));
            tween(node)
                .to(0.3, { scale: new Vec3(1, 1, 1) })
                .start();
        } else {
            // zoomOut
            node.setScale(new Vec3(0.5, 0.5, 0.5));
            tween(node)
                .to(0.3, { scale: new Vec3(1, 1, 1) })
                .start();
        }
    }

    static printNodeHierarchy(node?: Node, level = 0, isLast = true, prefix = '') {
        if (!node) {
            node = find("Canvas")?.parent;
        }
        if (!node) {
            error("printNodeHierarchy node is null");
            return;
        }

        // 构建当前行的前缀
        let currentLine = prefix;
        if (level > 0) {
            currentLine += isLast ? '└── ' : '├── ';
        }
        console.log(currentLine + node.name);

        // 构建子节点的前缀
        const childPrefix = prefix + (level > 0 ? (isLast ? '    ' : '│   ') : '');

        // 遍历子节点
        const children = node.children;
        children.forEach((child, index) => {
            const isLastChild = index === children.length - 1;
            this.printNodeHierarchy(child, level + 1, isLastChild, childPrefix);
        });
    }
}

